package com.disdr.picture;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class 图片网格化 {


    private static int minX = 10000;
    private static int maxX = 0;
    private static int minY = 10000;
    private static int maxY = 0;

    public static void main(String[] args) throws Exception {

        File file = new File("src\\main\\resources\\templates\\杨林手写.jpg");
        BufferedImage bi = ImageIO.read(file);
        Graphics2D g2 = (Graphics2D) bi.getGraphics();
        g2.setColor(Color.black);
        Font font2 = new Font("", 0, 15);
        g2.setFont(font2);

        int width = bi.getWidth();
        int height = bi.getHeight();


        //加个网格：
        for(int x=0;x<width;x+=20){
            g2.drawLine(x, 0, x, height);
        }
        for(int y=0;y<height;y+=20){
            g2.drawLine(0, y, width, y);
        }
        //标记定位值
        for(int x=0;x<width;x+=60){
            g2.drawString(String.valueOf(x), x, 760);
        }
        for(int y=0;y<height;y+=20){
            g2.drawString(String.valueOf(y), 0, y);
        }

        g2.dispose();//释放对象

        ImageIO.write(bi, "jpg", new FileOutputStream("F:\\杨林手写1.jpg"));

    }

   public static BufferedImage getImg(String 字) throws IOException {
       /*File file = new File("src\\main\\resources\\templates\\杨林手写\\"+字+".jpg");
       if(!file.exists()){
           file = new File("src\\main\\resources\\templates\\杨林手写\\"+字+".png");
       }
       return ImageIO.read(file);*/

       File file = new File("src\\main\\resources\\templates\\杨林手写.jpg");
       BufferedImage bi = ImageIO.read(file);
       Integer[] obj = map.get(字);
       return bi.getSubimage(obj[0],obj[1],obj[2],obj[3]);
   }

   static Map<String,Integer[]> map = new HashMap<String,Integer[]>();

    static {
        map.put("1", new Integer[]{50, 280, 40, 100});
        map.put("2", new Integer[]{120, 280, 60, 100});
        map.put("3", new Integer[]{200, 280, 40, 100});
        map.put("4", new Integer[]{280, 280, 60, 100});
        map.put("5", new Integer[]{350, 280, 40, 100});
        map.put("6", new Integer[]{420, 280, 60, 100});
        map.put("7", new Integer[]{500, 280, 40, 100});
        map.put("8", new Integer[]{560, 280, 60, 100});
        map.put("9", new Integer[]{660, 280, 40, 100});
        map.put("0", new Integer[]{720, 280, 40, 100});

    }

}
